<?php


if (!empty($_SESSION['login'])&&!empty($_SESSION['email'])){
$db = new Mypdo();

$loyerManager = new LoyerManager($db);

$loyer = $loyerManager->getLoyerByMoisLocLoca(substr($_GET['date'],0,-3),$_GET['idl'],$_GET['idla']);

$date_appel = new DateTime($loyer->getDateLoyer());
$mois = array('01'=>'Janvier','02'=>'Fevrier','03'=>'Mars','04'=>'Avril','05'=>'Mai','06'=>'Juin','07'=>'Juillet','08'=>'Aout','09'=>'Septembre','10'=>'Octobre','11'=>'Novembre','12'=>'Decembre');

$mois_appel = $mois[$date_appel->format('m')];

$str_date_appel = $mois_appel.' '.$date_appel->format('Y');

$locManager = new LocationManager($db);
$locaManager = new LocataireManager($db);
$chargeManager = new ChargeManager($db);
$immeuManager = new ImmeubleManager($db);

$pers = $locaManager->getPersonne($_GET['idla']);
$loc = $locManager->getLoc($_GET['idl']);
$immeu = $immeuManager->getImmeu($loc->getIdImmeu());
$listeCharge = $chargeManager->getListByIdLoc($_GET['idl']);
$brutAPayer = $loc->getMontantLoc();
$netAPayer = $brutAPayer + $brutAPayer*($loc->getTvaLoc()/100);

foreach ($listeCharge as $value) {
    $montant = $value->getMontantCharge();

    $tva =  ($value->getTvaCharge()/100);
    $charge = ($montant + ($montant * $tva ));
    $netAPayer = $netAPayer+ $charge;
}

?>
<div>
    <h1>Appel de Loyer <?php echo $str_date_appel; ?></h1>
    <?php if($loyer->getStatutLoyer() == 0){
        ?><input id="acquit" name="acquit" type="button" onclick="document.location.href='index.php?page=211&idla=<?php echo $_GET['idla']; ?>&idl=<?php echo $_GET['idl']; ?>&date=<?php echo $date_appel->format('Y-m'); ?>'" value="Loyer Impayer" />
        <p>Reste à Payer : <?php echo $netAPayer-$loyer->getSoldePayerLoyer(); ?></p>
    <?php
    }else{
        ?><p>Statut : Loyer Payer</p><?php
    } ?>

    <h3>Location : <?php echo $loc->getNomLoc(); ?></h3>
    <p>Adresse : <?php echo $immeu->getAdresseImmeu(); ?></p>

    <h3>Locataire : </h3>
    <?php
    if(gettype($pers) != 'object'){
        foreach ($pers as $val) {
            ?><p><?php echo $val->getNomPers().' '.$val->getPrenomPers(); ?></p>
            <p>Adresse : <?php echo $val->getAdressePers(); ?></p>
        <?php
        }
    }else{
        ?><p><?php echo $pers->getNomPers().' '.$pers->getPrenomPers(); ?></p>
        <p>Adresse : <?php echo $pers->getAdressePers(); ?></p>
<?php }
    ?>


</div>
<h2>Liste des charges</h2>
<table class="tableau">
    <tr>
        <th>Nom Charge</th>
        <th>Montant</th>
        <th>TVA</th>
        <th>Montant TVA</th>
        <th>Montant TTC</th>
    </tr>
    <?php

foreach ($listeCharge as $value) {
    ?>
    <tr>
    <?php
    $montant = $value->getMontantCharge();

    $tva =  ($value->getTvaCharge()/100);
    $charge = ($montant + ($montant * $tva ));
    $netAPayer = $netAPayer+ $charge;

    ?>
        <td><?php echo $value->getNomCharge(); ?></td>
        <td><?php echo $value->getMontantCharge(); ?></td>
        <td><?php echo $value->getTvaCharge() ?></td>
        <td><?php echo $montant * $tva; ?></td>
        <td><?php echo $charge; ?></td>
    </tr>
    <?php

}


?>

</table>

<?php }else{
    header('Location: index.php');
}